#!/usr/bin/perl
#
# Reprocess one automach entry
#
use DBI;
use Mach;
use Counter::CGI;
use CGI::Carp;
use PHP::Session;


$q = new Counter::CGI;

my $sessid = $q->cookie("PHPSESSID");
my $session = PHP::Session->new($sessid, { create => 1 });


$machinekey = $q->param("uniqueid");

warn "fetching for key $machinekey\n";
 $stm = $Mach::dbh->prepare("SELECT lastmessage FROM automach
          WHERE uniqueid = $machinekey");

$stm->execute() || die "Select failed\n";
warn "Got ", $stm->rows(), " records\n";
while ($t = $stm->fetchrow_arrayref()) {
  Mach::processmessage($$t[0]);
}

returnwithmessage("Machine processed");

sub returnwithmessage {
    my $message = shift;
    warn "returning with message $message\n";
    $session->set("message" => $message);
    $returnurl = $session->get("showpage");
    $session->save();
    print $q->redirect($returnurl);
    print <<EoF;
<html><head>
<title>You should have been redirected</title>
</head><body>
You should not have seen this. You should have been at
<a href="$returnurl">$returnurl</a>.
<p>
Please report the bug to webmaster&#x40;counter.li.org.
</body>
</html>
EoF
    exit(0);
}
